---------------------------------------------------------------------------------
  -- Design Name : Inc/Dec Stack Pointer Register
  -- File Name   : ExMemSP.vhd
  -- Function    : Simple inc/dec 32b register file
  -- Authors     : Mirko Francuski  2006/0225
  --               Milos Mihajlovic 2006/0039
  -- School      : University of Belgrade
  --               School for Electrical Engineering
  --               Department for Computer Engineering and Information Theory
  -- Subject     : VLSI Computer Systems
---------------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use work.UserPkg.all;

entity ExMemSP is
    port (
      clk     : in  std_logic;
      cl      : in  std_logic;
      inc     : in  std_logic;
      dec     : in  std_logic;
      spOut   : out stackAddrExt := (others => '0')
    );
end ExMemSP;

architecture behavioral of ExMemSP is
  signal value    : stackAddrExt := (others => '0');
  signal valueInc : stackAddrExt := (others => '0');
  signal valueDec : stackAddrExt := (others => '0');

begin
  spOut <= valueDec when dec = '1' else
           value;
 
  valueInc <= value + 1;
  valueDec <= value - 1;

  process(clk)
  begin    
    if(falling_edge(clk)) then
      if (cl = '1') then
        value <= (others => '0');
      elsif (dec = '1') then        -- da li hocemo da kada je dec = inc = '1' popuje, ili da ne radi nista?
        value <= valueDec;
      elsif (inc = '1') then
        value <= valueInc;
      end if;
    end if;
  end process;

end architecture behavioral;
